﻿package org.edu.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.edu.bean.City;
import org.edu.bean.County;
import org.edu.bean.Province;
import org.edu.bean.Role;
import org.edu.bean.Role_user;
import org.edu.bean.User;
import org.edu.bean.UserInfo;
import org.edu.dao.UserDao;
import org.edu.dao.XZQHDao;
import org.edu.service.NavigationBarService;
import org.edu.service.UserService;
import org.edu.utils.StringDealUtil;
import org.edu.utils.UserUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserManagementController extends ActionSupport {
	
	@Resource
	XZQHDao xzqhDao ;
	@Resource
	UserService userService ;
	
	
	
	private User user;
	private Province province ;
	private City city ;
	private County county;
	private List<Role> allRoles ;
	private List<User> users;
	private int userId ;
	private String roleid ;
	private String result ;
	private int user_status ;
	private int proId ;
	private int cityId;
	private int countyId ;
	private boolean updateUserRole ;
	private UserInfo usinfo;
	
	private static final long serialVersionUID = 1L;

	
	public String informationManagement(){
//		if (null!=usinfo){
//			try {
//				userService.saveOrUpdate(usinfo);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
		User user=(User) ActionContext.getContext().getSession().get("user");
//		if (null!=user){
//			
//			UserInfo usinfo=null;
//			try {
//				usinfo = (UserInfo) userService.findUsInfoByUserId(user.getId());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			if (null==usinfo)
//			{
//			usinfo=new UserInfo();
//			usinfo.setUserId(user.getId());
//			}
//			ActionContext.getContext().put("usinfo", usinfo);
//		}
		
		List<User> lsu=userService.selectUsers(user);
		ActionContext.getContext().put("lsu",lsu);
		return SUCCESS ;
	}
	
	
	public String delUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		User session_user = (User)session.getAttribute("user");
		boolean status = false ;
		String meg = "" ;
		if(session_user.getId() != userId ){
			User user = new User();
			user.setId(userId);
			userService.delUser(user);
			status = true ;
		}else{
			meg = "不能删除自己的账号" ;
		}
		Map map = new HashMap();  
        map.put("status", status);
        map.put("meg", meg);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	public String accountManagement(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		User user = (User)session.getAttribute("user");
		users = userService.selectUsers(user);
		return SUCCESS ;
	}
	@SuppressWarnings("unchecked")
	public String ajaxFindUser(){
		User user = userService.selectUserByUser(userId);
		Map map = new HashMap();  
		boolean status = false ;
        if(user != null){
			status = true ;
			map.put("workProvinceId", user.getWorkProvinceId());
			map.put("workCityId",user.getWorkCityId());
			map.put("workCountyId", user.getWorkCountyId());
			map.put("systemNumber", user.getSystemNumber());
			map.put("userName", user.getUserName());
			map.put("userstatus", user.getStatus());
			map.put("status", status);
		}
        map.put("status", status);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS;
	}
	private List<Province> provinces ;
	@SuppressWarnings("unchecked")
	public String addUserPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		User user = (User)session.getAttribute("user");
		if(user.getWorkProvinceId()!= -1){
			provinces = xzqhDao.getProvinceById(user.getWorkProvinceId());
		}else{
			Province province = new Province();
			province.setProid(-1);
			province.setProname("全国");
			provinces= xzqhDao.getAllProvinces();
			provinces.add(province);
		}
		return SUCCESS ;
	}
	
	public String addUser(){
		if(userId > 0){
			user = userService.selectUserByUser(userId);
			updateUserRole = true ;
		}else {
			user.setSystemNumber(UserUtils.getSystemNum());
			user.setPassword(StringDealUtil.getMD5Str("123456"));
			userId = userService.createUser(user);
		}
		if(userId>0){
			if(user.getWorkProvinceId()!=-1){
				province = xzqhDao.getProvinceById(user.getWorkProvinceId()).get(0);
				if(user.getWorkCityId() != -1){
					city = xzqhDao.getCityByCiId(user.getWorkCityId()).get(0);
					if(user.getWorkCountyId() != -1){
						county = xzqhDao.getCountyByCoId(user.getWorkCountyId()).get(0);
					}else{
						county = new County() ;
						county.setCountyid(-1);
						county.setCountyname("全市");
					}
				}else {
					city= new City();
					city.setCityid(-1);
					city.setCityname("全省");
				}
			}else{
				province = new Province() ;
				province.setProid(-1);
				province.setProname("全国");
			}
			allRoles = userService.getAllRoles();
			return SUCCESS ;
		}else{
			return ERROR ;
		}

	}

	@SuppressWarnings("unchecked")
	public String updateUser_status(){
		User user = userService.selectUserByUser(userId);
		user.setStatus(user_status);
		boolean changeStatus = userService.updateUser(user);
		Map map = new HashMap();
		map.put("status", changeStatus);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	
	@SuppressWarnings("unchecked")
	public String updateUser_pro(){
		User user = userService.selectUserByUser(userId);
		user.setWorkProvinceId(proId);
		user.setWorkCityId(-1);
		user.setWorkCountyId(-1);
		boolean status = userService.updateUser(user);
		Map map = new HashMap();
		map.put("status", status);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	public String updateUser_City(){
		User user = userService.selectUserByUser(userId);
		user.setWorkCityId(cityId);
		user.setWorkCountyId(-1);
		boolean status = userService.updateUser(user);
		Map map = new HashMap();
		map.put("status", status);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	public String updateUser_County(){
		User user = userService.selectUserByUser(userId);
		user.setWorkCountyId(countyId);
		boolean status = userService.updateUser(user);
		Map map = new HashMap();
		map.put("status", status);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	public String addUserRole(){
		String[] roleArr = roleid.split(",");
		userService.userAddRole(userId, roleArr);
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String getUserRole(){
		int rId = Integer.parseInt(roleid);
		List<Role_user> roleUsers = userService.selectRoleUsers(userId, rId);
		boolean status = false;
		if(roleUsers.size()>0){
			status = true ;
		}
		Map map = new HashMap();
		map.put("status", status);
		JSONObject object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	public List<Province> getProvinces() {
		return provinces;
	}


	public void setProvinces(List<Province> provinces) {
		this.provinces = provinces;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Province getProvince() {
		return province;
	}

	public void setProvince(Province province) {
		this.province = province;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public County getCounty() {
		return county;
	}

	public void setCounty(County county) {
		this.county = county;
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(List<Role> allRoles) {
		this.allRoles = allRoles;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getRoleid() {
		return roleid;
	}

	public void setRoleid(String roleid) {
		this.roleid = roleid;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public int getUser_status() {
		return user_status;
	}

	public void setUser_status(int userStatus) {
		user_status = userStatus;
	}

	public int getProId() {
		return proId;
	}

	public void setProId(int proId) {
		this.proId = proId;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public int getCountyId() {
		return countyId;
	}

	public void setCountyId(int countyId) {
		this.countyId = countyId;
	}

	public boolean isUpdateUserRole() {
		return updateUserRole;
	}

	public void setUpdateUserRole(boolean updateUserRole) {
		this.updateUserRole = updateUserRole;
	}


	public UserInfo getUsinfo() {
		return usinfo;
	}


	public void setUsinfo(UserInfo usinfo) {
		this.usinfo = usinfo;
	}

}
